package com.redpack.redpost.struts.action.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.redpack.redpost.struts.form.admin.agregarSubseccionForm;
import com.redpack.redpost.util.dao.adminDAO;
import com.redpack.redpost.util.to.generalUtil;
import com.redpack.redpost.util.to.subseccionesTo;

public class editarsubseccion extends Action {
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		boolean logginVal = generalUtil.isLoggedInAdmin(request);
		if(logginVal!=true)
			return mapping.findForward("noLogin") ;
		
		agregarSubseccionForm f = (agregarSubseccionForm)form;
		
		if( f != null ){
			if( f.getAcc().equals("2") ){
				adminDAO.eliSubseccion(f.getCve_subseccion());
			}
			
			if( f.getAcc().equals("1") ){
				String[][] idiomas = generalUtil.obtieneIdiomas();
				String[][] secciones = generalUtil.obtieneSecciones();
				request.setAttribute("idiomas", idiomas);
				request.setAttribute("secciones", secciones);
		
				subseccionesTo subseccion_info = adminDAO.getSubseccion( f.getCve_subseccion() );
				
				request.setAttribute("nombre_sel", subseccion_info.getNombre());
				request.setAttribute("cve_subseccion", ""+subseccion_info.getCve_subseccion());
				request.setAttribute("seccion_sel", subseccion_info.getSeccion());
				request.setAttribute("idioma_sel", subseccion_info.getIdioma());
				request.setAttribute("orden_sel", subseccion_info.getOrden()+"");
				
				return mapping.findForward("edit") ;
			}
		}
		
		List subsecciones = adminDAO.getSubsecciones();
		request.setAttribute("subsecciones", subsecciones);
		
		return mapping.findForward("success") ;
	}

}